package demo;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Logger;

import util.Configuration;
import util.FileNameUtil;
import util.po.ConfigPO;

public class Server2Receive implements Runnable {

	private Socket client;

	private Socket serverClient;

	private Logger log = Logger.getLogger(Server2Receive.class.getName());

	private ConfigPO config = new Configuration().configure();

	public Server2Receive(Socket client, Socket serverClient) {
		this.client = client;
		this.serverClient = serverClient;
	}

	// 接收文件
	private String receiver(Socket client, Socket client2) {
		String fileName = "";
		try {

			if (null != client) {
				log.info("begin receiver ");
				client.setReceiveBufferSize(config.getReceiveBufferSize() * 10);
				BufferedInputStream input = new BufferedInputStream(
						client.getInputStream());
				DataInputStream dis = new DataInputStream(input);
				fileName = config.getServer2SocketReceive() + File.separator
						+ FileNameUtil.getFileName()
						+ config.getReceiveExtName();
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(fileName));
				DataOutputStream dos = new DataOutputStream(bos);
				BufferedOutputStream bosSend = new BufferedOutputStream(
						client2.getOutputStream());
				int len = 0;
				int i = 0;
				while ((len = dis.read()) != -1) {
					++i;
					dos.write((char) len);
					bosSend.write((char) len);
					if (0 == (len % client.getReceiveBufferSize())) {
						dos.flush();
					}
				}
				System.out.println("total send " + i);
				bosSend.close();
				input.close();
				dos.close();
				bos.close();
				client.close();
				client2.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return fileName;
	}

	// 发送文件给Socket服务端
	@SuppressWarnings("unused")
	@Deprecated()
	/**
	 * Description please use receiver
	 * from socket get input stream send server socket
	 * @param socket serverSocket
	 * @param fileName saved filename
	 */
	private void send(Socket socket, String fileName) {
		try {
			if (null != socket) {
				log.info("begin execute send");
				BufferedOutputStream output = new BufferedOutputStream(
						socket.getOutputStream());
				DataInputStream dis = new DataInputStream(
						new BufferedInputStream(new FileInputStream(fileName)));
				socket.setReceiveBufferSize(config.getSendBufferSize() * 10);
				DataOutputStream dos = new DataOutputStream(output);
				int len = 0;
				int i = 0;
				while ((len = dis.read()) > 0) {
					dos.write((char) len);
					++i;
				}
				log.warning("total send " + i);
				dis.close();
				dos.flush();
				dos.close();
				output.flush();
				output.close();
				socket.close();
				log.info("file send successful ");
			}
		} catch (IOException ex) {
			log.warning(ex.getLocalizedMessage());
			ex.printStackTrace();
		}
	}

	@Override
	public void run() {
		// String fileName =
		receiver(client, serverClient);
		// send(serverClient, fileName);

	}

}
